<?php

class Users_ProfileController extends Zend_Controller_Action {

    public function init() {
        $userValidation = new Validations_User();
        $valid = $userValidation->hasidentity();
        if (!$valid) {
            $this->_redirect('/user/login');
        }
    }

    public function indexAction() {
        // action body
    }

    public function listusersAction() {
        $userValidation = new Validations_User();
        $usersDb = new Users_Model_Users();
        $result = $usersDb->fetchAllUserByType('user');
        $this->view->allUsers = $result;
        $this->view->currUser = $userValidation->getUserId();
    }

    public function updateAction() {
        $form = new Users_Form_Userprofileupdate();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                print_r($values);
                $db = new Users_Model_Users();
                $validation = new Validations_User();
                $name = $values['name'];
                $address = json_encode(array('address' => $values['address']));
                $contact = json_encode(array('contact' => array('primary_contact' => $values['primary_contact'], 'secondary_contact' => $values['sec_contact'])));
                $details = json_encode(array('details' => array($contact, $address)));
                $status = $values['status'];
                $data = array(
                    'name' => $name,
                    'details' => $details,
                    'status' => $status
                );
                $db->updateUser($data, $validation->getUserId());
                $this->_redirect('/home');
            }
        }
    }

    public function passwordAction() {
        $form = new Users_Form_Userpassword();
        $this->view->form = $form;
        $formErrors = array();
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $validation = new Validations_User();
                $password = $validation->getInstanceData()->password;
                $old = $values['old'];
                $new1 = $values['new1'];
                $new2 = $values['new2'];

                if ($password != md5($old)) {// check for old password
                    array_push($formErrors, 'Old password not matched.');
                } else {
                    if ($new1 != $new2) {
                        array_push($formErrors, 'New Passwords are not matching.');
                    } else {
                        $db = new Users_Model_Users();
                        $data = array('password' => md5($new1));
                        $db->updateUser($data, $validation->getUserId());
                        $this->view->success = 'Password Changed Successfully.';
                    }
                }
            }
            if (sizeof($formErrors) > 0) {
                $this->view->formErrors = $formErrors;
            }
        }
    }

    public function publicAction() {
        $validation = new Validations_User();
        if ($validation->hasidentity()) {
            $id = $this->getinputidAction();
            $user = new Users_Model_Users();
            $details = $user->getUserById($id);
            if ($details[0]['name'] != null) {
                $name = $details[0]['name'];
            } else {
                $name = '';
            }
            $decodedDetails = $user->getUserDetailsDecoded($details[0]['details']);
            $this->view->id = $id;
            $this->view->name = $name;
            $this->view->email = $details[0]['email'];
            $this->view->status = $details[0]['status'];
            $this->view->contact1 = $decodedDetails['contact1'];
            $this->view->contact2 = $decodedDetails['contact2'];
            $this->view->address = $decodedDetails['address'];
        } else {
            $this->_redirect('/user/login');
        }
    }

    /**
     * 
     * @return type
     */
    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id;
        return $id;
    }

}

